/*
 * QStringListAdapter.cpp  0.4.0 / Nov 14, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "QStringListAdapter.h"

#include "JNIWrapper.h"
#include "QStringAdapter.h"

// QStringListAdapter :: Public Constructor. -----------------------------------

/*!
 * \brief Constructs new QStringListAdapter object.
 *
 * \param env the JNIEnv pointer
 * \param stringArray the Java string array
 *
 * \exception OutOfMemoryError java error
 */
QStringListAdapter::QStringListAdapter(JNIEnv *env, jobjectArray stringArray)
  : QStringList()
{
  Q_ASSERT(env);
  Q_ASSERT(stringArray);
  jsize arrayLen = env->GetArrayLength(stringArray);
  for (int i = 0; i < arrayLen; i++) {
    append(QStringAdapter(env,
                          (jstring)env->GetObjectArrayElement(stringArray, i)));
  }
}

// QStringListAdapter :: Public Static Member Function. ------------------------

/*!
 * \brief Creates new Java string array using specified QStringList.
 *
 * \param env the JNIEnv pointer
 * \param stringList the QStringList reference
 *
 * \return Java string array
 *
 * \exception OutOfMemoryError java error
 */
jobjectArray QStringListAdapter::toJStringArray(JNIEnv *env,
                                                const QStringList &stringList)
{
  jobjectArray stringArray = JNIWrapper::newStringArray(env, stringList.size());
  if (stringArray) {
    for (int i = 0; i < stringList.size(); i++) {
      env->SetObjectArrayElement(stringArray, i,
                                 QStringAdapter::toJString(env,
                                                           stringList.at(i)));
    }
  }
  return stringArray;
}
